package com.hh.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hh.commonutils.MD5;
import com.hh.commonutils.Result;
import com.hh.commonutils.jwt.JwtUtil;
import com.hh.usercenter.entity.UcenterMember;
import com.hh.usercenter.entity.vo.RegisterVo;
import com.hh.usercenter.mapper.UcenterMemberMapper;
import com.hh.usercenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author hh
 * @since 2021-12-22
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private UcenterMemberMapper ucenterMemberMapper;
    @Override
    public Result register(RegisterVo registerVo) {
        // 校验
        if (StringUtils.isEmpty(registerVo)) {
            return Result.fail().data("msg", "注册信息为空!");
        }

        // 校验 验证码
        String code = registerVo.getCode() != null ? registerVo.getCode() : "";
        String mobile = registerVo.getMobile() != null ? registerVo.getMobile() : "";

        String codeinRedis = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(codeinRedis)) {
            return Result.fail().data("msg", "验证码错误！请稍后再试！");
        }

        // 校验 是否注册过
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile", mobile);
        int integer = ucenterMemberMapper.selectCount(ucenterMemberQueryWrapper);
        if (integer > 0) {
            return Result.fail().data("msg", "当前手机号已注册，请进行登录!");
        }

        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(registerVo.getMobile());
        ucenterMember.setNickname(registerVo.getNickname());
        ucenterMember.setPassword(MD5.encrypt(registerVo.getPassword()));

        int insert = ucenterMemberMapper.insert(ucenterMember);
        if (insert > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    // 登陆成功返回token
    @Override
    public String login(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String oldPassword = registerVo.getPassword();

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(oldPassword)) {
            throw new RuntimeException("账号或密码不能为空!");
        }

        String password = MD5.encrypt(oldPassword);

        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile", mobile);
        ucenterMemberQueryWrapper.eq("password", password);
        UcenterMember ucenter = ucenterMemberMapper.selectOne(ucenterMemberQueryWrapper);

        if (ucenter == null) {
            throw new RuntimeException("账号或密码错误，请重试");
        }

        String jwtToken = JwtUtil.getJwtToken(ucenter.getId(), ucenter.getNickname());
        return jwtToken;
    }

    @Override
    public UcenterMember getInfoByToken(HttpServletRequest request) {
        UcenterMember ucenterMember = new UcenterMember();
        boolean flag = JwtUtil.checkToken(request);
        if (!flag){
            throw new RuntimeException("该token已失效!");
        } else {
            String memberIdByJwtToken = JwtUtil.getMemberIdByJwtToken(request);
            ucenterMember = ucenterMemberMapper.selectById(memberIdByJwtToken);
        }
        return ucenterMember;
    }

    @Override
    public UcenterMember getInfoByOpenId(String openId) {
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("openid", openId);
        UcenterMember ucenterMember = ucenterMemberMapper.selectOne(ucenterMemberQueryWrapper);
        return ucenterMember;
    }

    // 根据会员Id获取会员信息
    @Override
    public UcenterMember getUcenterMenberById(String id) {
        UcenterMember ucenterMember = ucenterMemberMapper.selectById(id);
        return ucenterMember;
    }

    // 统计某天注册的人数
    @Override
    public int getRegisterCountOfDay(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("时间参数格式错误！");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.select("count(id)");
        wrapper.like("gmt_create", date);
        int count = ucenterMemberMapper.selectCount(wrapper);
        return count;
    }
}
